package android.cs2340UDT;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.*;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import android.cs2340UDT.Security;

 /*
  * This is the main class so that when the app first opens, it will display the login screen
  */
public class CS2340AndroidUDTActivity extends Activity {
    // Initializing variables
    EditText inputName;
    EditText inputPassword;
    
    static boolean launch = true;
    private static Security security;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Displays the login screen
        setContentView(R.layout.loginscreen);
        
        if(launch) {
        	security = new Security(this);
        	launch = false;
        }
 
        inputName = (EditText) findViewById(R.id.username);
        inputPassword = (EditText) findViewById(R.id.password);
        Button NewAccountButton = (Button) findViewById(R.id.NewAccountButton);
 
        //Listening to Register New Account Button button event
        NewAccountButton.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View arg0) {
                //Starting a new Intent
                Intent nextScreen = new Intent(getApplicationContext(), CreateAccountActivity.class);
 
               // Sending data to another Activity
                nextScreen.putExtra("name", inputName.getText().toString());
                nextScreen.putExtra("password", inputPassword.getText().toString());
 
                Log.e("n", inputName.getText()+"."+ inputPassword.getText());
 
                startActivity(nextScreen);
 
            }
        });
        
        final EditText inputUsername = (EditText) findViewById(R.id.username);
        final EditText inputPassword = (EditText) findViewById(R.id.password);
        Button LoginButton = (Button) findViewById(R.id.LoginButton);
        
        //Listening to Register New Account Button button event
        LoginButton.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View arg0) {
            	
            	if(security.checkLogin(inputUsername.getText().toString(), inputPassword.getText().toString())) {
            		// successful login, switch to the user's main screen
        
            		Intent nextScreen = new Intent(getApplicationContext(), MainScreenActivity.class);
            		
            		finish();
            		
                    startActivity(nextScreen);
            	}
            	else {
            		AlertDialog.Builder aDialog = new AlertDialog.Builder(CS2340AndroidUDTActivity.this);
            		aDialog.setMessage("Invalid Login");
            		aDialog.show();
            		
            	}
 
            }
        });
    }
    
    /**
     * Getter that returns security.
     * @return security The current security item.
     */
    static  Security getSecurity() {
    	return security;
    }
}